package cn.edu.ctbu.scoremg.service;

import cn.edu.ctbu.scoremg.dao.StudentRepository;
import cn.edu.ctbu.scoremg.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class StudentService {
    @Autowired
    public StudentRepository studentRepository;
    public List<Student> findAll() { return studentRepository.findAll(); }
    public Page<Student> getAll(Pageable pageable) { return studentRepository.findAll(pageable); }
    public Page<Student> getAll(Example<Student> example, Pageable pageable) {
        return studentRepository.findAll(example, pageable); }
    /**
     * 按id进行查询
     * @param id 主键，自增
     * @return
     */
    public Student findById(Integer id) {
        return studentRepository.findById(id).get();
    }
    /**
     * 按姓名进行查询
     * @param name
     * @return
     */
    public List<Student> findByNameLike(String name) {
        return studentRepository.findByNameLike(name); }
    /**
     *按名字和学号进行查询
     * @param name
     * @param sno
     * @return
     */
    public List<Student> findByNameAndsno(String name, String sno) {
        return studentRepository.findByNameAndSno(name, sno);
    }
    /**根据学号查询*/
    public List<Student> findBySno(String sno) { return studentRepository.findBySno(sno); }

    /**新增学生*/
    public Student insert(Student student) {
        return studentRepository.save(student);
    }
    /**更新学生*/
    public Student update(Student student) {
        return studentRepository.save(student);
    }
    /**根据id删除学生*/
    public void delete(Integer id) {
        studentRepository.deleteById(id);
    }
    /**根据id批量删除*/
    @Transactional
    public void deleteByIds(List<Integer> ids) {
        studentRepository.deleteByIds(ids);
    }

}
